<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html">

<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
    <title>乐优商城--购物车页面</title>

    <link rel="stylesheet" type="text/css" href="css/webbase.css"/>
    <link rel="stylesheet" type="text/css" href="css/pages-cart.css"/>
</head>

<body>

<div id="cartApp">

    <div class="top">
        <shortcut/>
    </div>

    <div class="cart py-container">

        <!--logoArea-->
        <div class="logoArea">
            <div class="fl logo"><span class="title">购物车</span></div>
        </div>

        <!--All goods-->
        <div class="allgoods">
            <h4>全部商品<span>11</span></h4>
            <div class="cart-main">
                <div class="yui3-g cart-th">
                    <div class="yui3-u-1-4"><input type="checkbox" v-model="slt" @click="chooseAll()"/> 全部</div>
                    <div class="yui3-u-1-4">商品</div>
                    <div class="yui3-u-1-8">单价（元）</div>
                    <div class="yui3-u-1-8">数量</div>
                    <div class="yui3-u-1-8">小计（元）</div>
                    <div class="yui3-u-1-8">操作</div>
                </div>
                <div class="cart-item-list">

                    <div class="cart-body">
                        <div class="cart-list">

                            <ul class="goods-list yui3-g" v-for="(cart,i) in carts" :key="i" v-if="cart.saleable">
                                <li class="yui3-u-1-24">
                                    <input type="checkbox" v-model="selectedCarts" :value="cart" />
                                </li>
                                <li class="yui3-u-11-24">
                                    <div class="good-item">
                                        <div class="item-img"><img :src="cart.image" width="80px" height="80px"/></div>
                                        <div class="item-msg">
												<span style="line-height:70px ">
													{{cart.title}}&emsp;&emsp;
                                                </span>
                                                <span v-for="(v,k,j) in JSON.parse(cart.ownSpec)" :key="j">{{v}}&emsp;</span>
                                        </div>
                                    </div>
                                </li>

                                <li class="yui3-u-1-8">
                                    <span v-if="cart.price > cart.realPrice"><font color="red">比加入时降：{{ly.formatPrice(cart.price - cart.realPrice)}}</font></span>
                                    <span style="line-height:70px " class="price">
                                    {{ly.formatPrice(cart.realPrice)}}
                                    </span>

                                </li>
                                <li class="yui3-u-1-8" style="padding-top: 20px">
                                    <a href="javascript:void(0)" class="increment mins" @click.prevent="minus(cart)">-</a>
                                    <input autocomplete="off" type="text" v-model="cart.num" value="1" minnum="1" class="itxt" />
                                    <a href="javascript:void(0)" class="increment plus" @click.prevent="plus(cart)">+</a>
                                </li>
                                <li class="yui3-u-1-8"><span style="line-height:70px " class="sum">{{ly.formatPrice(cart.num * cart.realPrice)}}</span></li>
                                <li class="yui3-u-1-8">
                                    <a href="#none" @click.prevent="deleteCart(i)">删除</a><br />
                                    <a href="#none" v-if="isAttention" @click.prevent="transferToCollect(i,cart)">移到我的关注</a>
                                </li>
                            </ul>

                            <hr/>
                            <ul class="goods-list yui3-g" style="background-color: Lavender" v-for="(cart,i) in carts" :key="i" v-if="!cart.saleable">
                                <li class="yui3-u-1-24">
                                    <br/>
                                    <br/>
                                    <span style="color: grey">已失效</span>
                                </li>
                                <li class="yui3-u-11-24">
                                    <div class="good-item">
                                        <div class="item-img"><img :src="cart.image" width="80px" height="80px"/></div>
                                        <div class="item-msg">
												<span style="line-height:70px ">
													{{cart.title}}&emsp;&emsp;
                                                </span>
                                            <span v-for="(v,k,j) in JSON.parse(cart.ownSpec)" :key="j">{{v}}&emsp;</span>
                                        </div>
                                    </div>
                                </li>

                                <li class="yui3-u-1-8">
                                    <br/>
                                    <span style="color: black;">
                                    宝贝已无法购买，
                                    </span><br/>
                                    <span style="color: black;">
                                    请联系卖家或重新选购
                                    </span>

                                </li>
                                <li class="yui3-u-1-8" style="padding-top: 20px">
                                    <a href="javascript:void(0)" class="increment mins" >-</a>
                                    <input disabled="disabled" type="text" value="--" class="itxt" />
                                    <a href="javascript:void(0)" class="increment plus" >+</a>
                                </li>
                                <li class="yui3-u-1-8">
                                    <span style="line-height:70px " class="sum">∞</span>

                                </li>
                                <li class="yui3-u-1-8">
                                    <a href="#none" @click.prevent="deleteCart(i)">删除</a>
                                    <br/>
                                    <a href="#" @click.prevent="searchLike(cart.title.split(' ',2))"><h5 style="color: red;">找相似</h5></a>
                                    
                                    <h4>该商品已下架</h4>
                                </li>
                            </ul>

                        </div>
                    </div>
                </div>

            </div>
            <div class="cart-tool">
                <div class="select-all">
                    <input type="checkbox" name="" id="" value=""/>
                    <span>全选</span>
                </div>
                <div class="option">
                    <a href="#none">删除选中的商品</a>
                    <a href="#none">移到我的关注</a>
                    <a href="#none">清除下柜商品</a>
                </div>
                <div class="toolbar">
                    <div class="chosed">已选择<span>{{totalGoodsNum}}</span>件商品</div>
                    <div class="sumprice">
                        <span><em>总价（不含运费） ：</em><i class="summoney">{{ly.formatPrice(totalPrice)}}</i></span>
                        <span ><em>已节省：</em><i>-¥{{ly.formatPrice(savePrice)}}</i></span>
                    </div>
                    <div class="sumbtn">
                        <a class="sum-btn"  href="getOrderInfo.html"  target="_blank">结算</a>
                    </div>
                </div>
            </div>
            <div class="clearfix"></div>
            <div class="deled">
                <span>已删除商品，您可以重新购买或加关注：</span>
                <div class="cart-list del">
                    <ul class="goods-list yui3-g" v-for="(dcart,index) in deleteCarts" :key="index">
                        <li class="yui3-u-1-2">
                            <div class="good-item">
                                <div class="item-msg">
                                    {{dcart.title.length <= 46 ? dcart.title : dcart.title.substring(0,46) + "..."}}&emsp;
                                    <span v-for="(v,k,j) in JSON.parse(dcart.ownSpec)" :key="j">
                                        {{v}}
                                        <span v-if="k != '13' ">/</span>
                                    </span>
                                </div>
                            </div>
                        </li>
                        <li class="yui3-u-1-6"><span class="price">{{ly.formatPrice(dcart.price)}}</span></li>
                        <li class="yui3-u-1-6">
                            <span class="number">{{dcart.num}}</span>
                        </li>
                        <li class="yui3-u-1-8">
                            <a href="#none" @click.prevent="buyAgain(dcart,index)">重新购买</a>
                            <a href="#none" v-if="isAttention">移到我的关注</a>
                        </li>
                    </ul>
                </div>
            </div>
            <div class="liked">
                <ul class="sui-nav nav-tabs">
                    <li class="active">
                        <a href="#index" data-toggle="tab">猜你喜欢</a>
                    </li>
                    <li>
                        <a href="#profile" data-toggle="tab">特惠换购</a>
                    </li>
                </ul>
                <div class="clearfix"></div>
                <div class="tab-content">
                    <div id="index" class="tab-pane active">
                        <div id="myCarousel" data-ride="carousel" data-interval="4000" class="sui-carousel slide">
                            <div class="carousel-inner">
                                <div class="active item">
                                    <ul>
                                        <li>
                                            <img src="img/like1.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                        <li>
                                            <img src="img/like2.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                        <li>
                                            <img src="img/like3.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                        <li>
                                            <img src="img/like4.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                    </ul>
                                </div>
                                <div class="item">
                                    <ul>
                                        <li>
                                            <img src="img/like1.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                        <li>
                                            <img src="img/like2.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                        <li>
                                            <img src="img/like3.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                        <li>
                                            <img src="img/like4.png"/>
                                            <div class="intro">
                                                <i>Apple苹果iPhone 6s (A1699)</i>
                                            </div>
                                            <div class="money">
                                                <span>$29.00</span>
                                            </div>
                                            <div class="incar">
                                                <a href="#" class="sui-btn btn-bordered btn-xlarge btn-default"><i
                                                        class="car"></i><span class="cartxt">加入购物车</span></a>
                                            </div>
                                        </li>
                                    </ul>
                                </div>
                            </div>
                            <a href="#myCarousel" data-slide="prev" class="carousel-control left">‹</a>
                            <a href="#myCarousel" data-slide="next" class="carousel-control right">›</a>
                        </div>
                    </div>
                    <div id="profile" class="tab-pane">
                        <p>特惠选购</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

</div>


<script src="./js/vue/vue.js"></script>
<script src="./js/axios.min.js"></script>
<script src="./js/common.js"></script>
<script type="text/javascript">
    var cartVm = new Vue({
        el: "#cartApp",
        data: {
            ly,
            carts: [],  // 购物车
            selectedCarts: [],  // 选择的cart集合
            slt: true,
            deleteCarts: [],
            cartsSkus: [],  // 购物车中skuId对应数据库中最新的skus集合
            availableCarts:[],
            isAttention: false

        },
        created(){
           this.loadData();


        },
        computed:{
            totalGoodsNum(){
                return this.selectedCarts.reduce((c1,c2)=>c1 + c2.num,0);
            },
            totalPrice(){
                return this.selectedCarts.reduce((c1,c2)=>c1 + c2.num * c2.realPrice,0);
            },
            savePrice(){
                let ppp = 0;
                this.selectedCarts.forEach(c=>{
                    if (c.realPrice < c.price){
                        ppp = ppp + (c.price - c.realPrice) * c.num;
                    }
                })
                return ppp;
            }
        },
        watch: {
            selectedCarts: {
                handler(){
                    if(this.selectedCarts.length != this.availableCarts.length){
                        this.slt = false;
                    }else{
                        this.slt = true;
                    }
                },
                deep: true
            }
        },
        methods: {
            loadData(){
                ly.verify().then(res=>{
                    this.isAttention = true;
                    // 已登录状态
                    // 先查看localstorage中是否有购物车数据
                    const locCarts = ly.store.get("LY_CARTS") || [];
                    if (locCarts[0]){
                        console.log(locCarts);
                        // localstorage中有购物车数据，保存至后台Redis中                       
                        let localc = [];
                        locCarts.forEach(c=>{
                            localc.push({
                                skuId: c.skuId,
                                num: c.num
                            })
                        })                       
                        ly.http.post("cart/addLocalcarts",localc).then(({data})=>{
                            this.carts = data;                           
                            this.carts.forEach(cart=>{
                                if (cart.saleable) {
                                    this.availableCarts.push(cart);
                                }
                            })
                            this.selectedCarts = this.availableCarts;

                            // 添加成功，删除localstorage中数据
                            window.localStorage.clear();
                        })

                    }else{
                        ly.http.get("/cart/getCartsOfUser").then(({data})=>{

                            this.carts = data;
                            this.carts.forEach(cart=>{
                                if (cart.saleable) {
                                    this.availableCarts.push(cart);
                                }
                            })
                            this.selectedCarts = this.availableCarts;

                        })
                    }


                }).catch(()=>{
                    // 未登录状态
                    // 从localstorage中获取购物车信息

                    const ccc = ly.store.get("LY_CARTS") || [];


                    let ids = [];
                    ccc.forEach(cart=>{
                        ids.push(cart.skuId);
                    })

                    ly.http.get("/cart/selectedSkus",{
                        params:{
                            idds: ids.join(",")
                        }
                    }).then(({data})=>{
                        this.cartsSkus = data;

                        ccc.forEach(c=>{
                            c.realPrice = this.cartsSkus.find(sku => sku.id == c.skuId).price;
                        });
                        this.carts = ccc;
                        this.availableCarts = this.carts;
                        this.selectedCarts = this.availableCarts;
                    })


                });
                this.deleteCarts = JSON.parse(window.sessionStorage.getItem("DEL_CARTS")) || [];
            },
<<<<<<< HEAD
=======
<<<<<<< HEAD
            loadCart(){
                ly.verify().then(res=>{
                    // 已登录购物车，查询

                    ly.http.get("cart").then(({data})=>{
                        this.carts = data;
                        this.selectedCart = this.carts;
=======
>>>>>>> hebing
            transferToCollect(ic,cart){
                if (confirm("确认移动到我的关注？")) {
                    ly.http.post("cart/transferToCollect",ly.stringify({skuId: cart.skuId})).then(()=>{
                        // 移除购物车中对应商品
                        this.carts.splice(ic,1);
                        // 移除availableCarts和selectedCarts中对应商品
                        let ia = this.availableCarts.indexOf(cart);
                        this.availableCarts.splice(ia,1);
                        let is = this.selectedCarts.indexOf(cart);
                        if (is > -1){
                            this.selectedCarts.splice(is,1);
                        }
<<<<<<< HEAD
=======
>>>>>>> master
>>>>>>> hebing

                    })
                }
            },
            plus(cart){
                if (cart) {
                    cart.num++;
                }
                ly.verify().then(res=>{
                    // 已登录，向后台发起请求
                    ly.http.put("cart/updateCartNum",ly.stringify({skuId:cart.skuId, num:cart.num}));
                }).catch(()=>{
                    // 未登录，操作localstorage
                    ly.store.set("LY_CARTS",this.carts);
                })
            },
            minus(cart){
                if(cart && cart.num > 1){
                    cart.num--;
                }else{
                    return;
                }
                ly.verify().then(res=>{
                    // 已登录，向后台发起请求
                    ly.http.put("cart/updateCartNum",ly.stringify({skuId:cart.skuId, num:cart.num}));
                }).catch(()=>{
                    // 未登录，操作localstorage
                    ly.store.set("LY_CARTS",this.carts);
                })
            },
            deleteCart(i){
                if (confirm("确认删除该商品吗？")){

                    ly.verify().then(res=>{
                        // 已登录，向后台发送请求
                        ly.http.delete("cart/deleteCartOfUser",{params:{skuId: this.carts[i].skuId}}).then(
                            ()=>{

                                // 将删除商品保存至删除商品集中
                                this.deleteCarts.push(this.carts[i]);

                                // 删除索引对应的元素
                                this.carts.splice(i,1);

                                
                                // 更新sessionStorage中的数据
                                window.sessionStorage.setItem("DEL_CARTS",JSON.stringify(this.deleteCarts));
                            }
                        )
                    }).catch(()=>{
                        // 未登录
                        // 删除索引对应的元素
                        let delCart = this.carts.splice(i,1);
                        
                        // 更新localstorage中的数据
                        ly.store.set("LY_CARTS",this.carts);
                        // 将删除商品保存至删除商品集中
                        this.deleteCarts.push(delCart[0]);
                        window.sessionStorage.setItem("DEL_CARTS",JSON.stringify(this.deleteCarts));
                    })
                }else{
                    return;
                }
            },
            searchLike(termInfo){

                window.location.href = "http://www.leyou.com/search.html?key="+termInfo;
            },
            chooseAll(){
                this.slt = !this.slt;
                if (this.slt) {
                    this.selectedCarts = this.availableCarts;
                }else{
                    this.selectedCarts = [];
                }
            },
            buyAgain(dcart,index){
                ly.verify().then(
                    // 已登录状态，发送信息至后台添加该购物车
                    ly.http.post("cart/addCarts",{skuId:dcart.skuId, num:dcart.num}).then(()=>{
                        // 将删除的购物车重新添加到购物购物车carts中
                        this.carts.push(dcart);
                        // 删除deleteCarts集合中对应的
                        this.deleteCarts.splice(index,1);
                        window.sessionStorage.setItem("DEL_CARTS",JSON.stringify(this.deleteCarts));
                    })
                ).catch(
                    // 未登录状态
                    ()=>{
                        // 将删除的购物车重新添加到购物购物车carts中
                        this.carts.push(dcart);
                        // 更新localstorage的购物车信息
                        ly.store.set("LY_CARTS",this.carts);
                        // 删除deleteCarts集合中对应的
                        this.deleteCarts.splice(index,1);
                        window.sessionStorage.setItem("DEL_CARTS",JSON.stringify(this.deleteCarts));
                    }
                )

            }
        },
        components: {
            shortcut: () => import("/js/pages/shortcut.js")
        }
    })
</script>
<!-- 底部栏位 -->
<!--页面底部，由js动态加载-->
<script type="text/javascript" src="js/plugins/jquery/jquery.min.js"></script>
<div class="clearfix footer"></div>
<script type="text/javascript">$(".footer").load("foot.html");</script>
<!--页面底部END-->
<script type="text/javascript" src="js/plugins/jquery.easing/jquery.easing.min.js"></script>
<script type="text/javascript" src="js/plugins/sui/sui.min.js"></script>
<script type="text/javascript" src="js/widget/nav.js"></script>

</body>

</html>